package com.sgh.springsecurity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @Description:
 * @Author: sgh
 * @Date: 2024/12/15
 */
@Configuration
public class SecurityConfig {

    /**
     * 密码加密
     * @return
     */
    @Bean
    public PasswordEncoder getPw() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 自定义登录密码
     * @return
     */
    @Bean
    public UserDetailsService userDetailsService() {
        System.out.println("自定义登录信息");
        UserDetails user = User.withUsername("admin")
                .password(getPw().encode("123"))
                .roles("admin", "normal")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}
